package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.AddedWashAdapter;
import com.wnhz.work.yz.bean.AllocationBean;
import com.wnhz.work.yz.utils.Tag;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---新增
 */
public class AddedWashActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;

    private ListView my_listView;
    private AddedWashAdapter mAdapter;

    private int position;

    private String cateId;

    private List<AllocationBean> beanList = new ArrayList<>();

    private AllocationBean bean;
    private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_added_wash);
        edit = (EditText) findViewById(R.id.ll_search2);
        edit.setVisibility(View.VISIBLE);
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                upload(s + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        position = getIntent().getIntExtra("position", 0);
        initData();
        initView();
    }

    private String packageId;

    private void initData() {
        cateId = getIntent().getStringExtra("cateId");
        packageId = getIntent().getStringExtra("packageId");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("新增");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        my_listView = (ListView) findViewById(R.id.my_listView);

        mAdapter = new AddedWashAdapter(this, this, beanList, my_listView);
        my_listView.setAdapter(mAdapter);

        upload("");

    }

    private static final String TAG = "AddedWashActivity";

    private void upload(String b) {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("keywords", b);
        map.put("cateId", cateId);
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        if (packageId != null) {
            map.put("packageId", packageId);
        }
        // MyToast(" " + packageId);
        XUtil.Post(Url.PACKAGE_ADDPACKAGEGOODS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
//                Log.e(TAG, "onSuccess:ADD== " + result.substring(0, result.length() / 4));
//                Log.e(TAG, "onSuccess:ADD== " + result.substring(result.length() / 4, (result.length() / 4 * 2)));
//                Log.e(TAG, "onSuccess:ADD== " + result.substring(result.length() / 4 * 2, (result.length() / 4 * 3)));
//                Log.e(TAG, "onSuccess:ADD== " + result.substring((result.length() / 4 * 3), result.length()));
                Log.e(" a" + cateId, result + "");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if (!"1".equals(res)) {
                        String info = jsonObject.optString("info");
                        MyToast("" + info);
                    }
                    JSONArray jsonArray = jsonObject.optJSONArray("info");

                    beanList.clear();
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            bean = new AllocationBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), AllocationBean.class);
                            beanList.add(bean);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //新增
            case R.id.tv_newAdd:
                int index = (int) v.getTag();
                if ("1".equals(beanList.get(index).getIs_sku())) {
                    bean = beanList.get(index);
//                    ActivityOptionsCompat compat = ActivityOptionsCompat.makeCustomAnimation(this,
//                            R.anim.push_up_in, R.anim.push_up_out);
                    ActivityCompat.startActivityForResult(this,
                            new Intent(this, MultipleSelectActivity.class)
                                    .putExtra("from", "AddedWashActivity")
                                    .putExtra("img", beanList.get(index).getGoods_pic())
                                    .putExtra("price", beanList.get(index).getGoods_price())
                                    .putExtra("goodsId", beanList.get(index).getGoods_id()), Tag.MULTIPLESELECT, null);
                } else {
                    beanList.get(index).setGoods_nums("1");
                    beanList.get(index).setIs_old("2");
                    intent = new Intent();
                    intent.putExtra("position", position);
                    intent.putExtra("data", beanList.get(index));
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Tag.MULTIPLESELECT:
                if (resultCode == RESULT_OK) {
                    String price = data.getStringExtra("price");
                    String skuInfo = data.getStringExtra("skuInfo");
                    String skuId = data.getStringExtra("skuId");
                    String isold = data.getStringExtra("isold");
//                    Log.e("==新增AddedWashActivity==", " 返回的数据 price=" + price + " = skuInfo=" + skuInfo + " = skuId= " + skuId);
                    bean.setGoods_price(price);
                    bean.setSku_info(skuInfo);
                    bean.setSku_id(skuId);
                    bean.setGoods_nums("1");
                    bean.setIs_old("2");
                    Log.e(TAG, "onActivityResult: " + bean.toString());

                    Intent intent = new Intent();
                    intent.putExtra("position", position);
                    intent.putExtra("data", bean);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;

            default:
                break;
        }
    }
}
